/**
 * @description router全局配置，如有必要可分文件抽离，其中asyncRoutes只有在intelligence模式下才会用到，pro版只支持remixIcon图标，具体配置请查看vip群文档
 */
import type { RouteRecordName, RouteRecordRaw } from 'vue-router'
import type { VabRouteRecord } from '/#/router'
import {
  createRouter,
  createWebHashHistory,
  createWebHistory,
} from 'vue-router'
import Layout from '@vab/layouts/index.vue'
import { setupPermissions } from './permissions'
import { authentication, isHashRouterMode, publicPath } from '@/config'

export const constantRoutes: VabRouteRecord[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/403',
    name: '403',
    component: () => import('@/views/403.vue'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/404.vue'),
    meta: {
      hidden: true,
    },
  },
]

export const asyncRoutes: VabRouteRecord[] = [
  {
    path: '/',
    name: 'Root',
    component: Layout,
    meta: {
      title: '首页',
      icon: 'home-2-line',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'index',
        name: 'Index',
        component: () => import('@/views/index/index.vue'),
        meta: {
          title: '首页',
          noClosable: true,
          icon: 'home-2-line',
        },
      },
    ],
  },
  {
    path: '/permission',
    name: 'Permission',
    component: Layout,
    meta: {
      title: '权限管理',
      icon: 'shield-keyhole-line',
      guard: ['Admin'],
    },
    children: [
      {
        path: 'adminInfo',
        name: 'AdminInfo',
        component: () =>
            import('@/views/operManagement/adminInfo/list/index.vue'),
        meta: {
          title: '管理员',
          icon: 'admin-line',
        },
      },
    ],
  },
  {
    path: '/enterpriseWeChat',
    name: 'EnterpriseWeChat',
    component: Layout,
    meta: {
      title: '企业微信客服',
      icon: 'customer-service-2-line',
    },
    children: [
      {
        path: 'limit',
        name: 'Limit',
        component: () =>
          import('@/views/enterpriseWeChat/limit/index.vue'),
        meta: {
          title: '可用额度',
          icon: 'money-cny-circle-line',
          guard: ['Admin'],
        },
      },
      {
        path: 'userPool',
        name: 'UserPool',
        component: () =>
          import('@/views/enterpriseWeChat/userPool/index.vue'),
        meta: {
          title: '用户账号池',
          icon: 'user-2-line',
        },
      },
      {
        path: 'categoryPool',
        name: 'CategoryPool',
        component: () =>
          import('@/views/enterpriseWeChat/categoryPool/index.vue'),
        meta: {
          title: '分类账号池',
          icon: 'list-ordered',
        },
      },
      {
        path: 'wechatCorp',
        name: 'WechatCorp',
        component: () =>
          import('@/views/enterpriseWeChat/wechatCorp/index.vue'),
        meta: {
          title: '企业授权',
          icon: 'building-line',
        },
      },
      {
        path: 'corpCategoryList',
        name: 'CorpCategoryList',
        component: () =>
          import('@/views/enterpriseWeChat/corpCategoryList/index.vue'),
        meta: {
          title: '账号分类',
          icon: 'list-check-2',
          guard: ['Admin'],
        },
      },
      {
        path: 'linkList',
        name: 'LinkList',
        component: () =>
          import('@/views/enterpriseWeChat/linkList/index.vue'),
        meta: {
          title: '获客链接',
          icon: 'links-fill',
        },
      },
      {
        path: 'recordList',
        name: 'RecordList',
        component: () =>
          import('@/views/enterpriseWeChat/recordList/index.vue'),
        meta: {
          title: '书籍记录',
          icon: 'book-read-line',
        },
      },
      {
        path: 'titleGalleryList',
        name: 'TitleGalleryList',
        component: () =>
          import('@/views/enterpriseWeChat/titleGalleryList/index.vue'),
        meta: {
          title: '标题图库分类',
          icon: 'apps-line',
        },
      },
      {
        path: 'titleList',
        name: 'TitleList',
        component: () =>
          import('@/views/enterpriseWeChat/titleList/index.vue'),
        meta: {
          title: '标题库',
          icon: 'text',
        },
      },
      {
        path: 'galleryList',
        name: 'GalleryList',
        component: () =>
          import('@/views/enterpriseWeChat/galleryList/index.vue'),
        meta: {
          title: '图库',
          icon: 'image-add-line',
        },
      },
      {
        path: 'orderList',
        name: 'OrderList',
        component: () =>
          import('@/views/enterpriseWeChat/orderList/index.vue'),
        meta: {
          title: '订单',
          icon: 'file-list-3-line',
          guard: ['Admin'],
        },
      },
      {
        path: 'userRankings',
        name: 'UserRankings',
        component: () =>
          import('@/views/enterpriseWeChat/userRankings/index.vue'),
        meta: {
          title: '客户数量排行榜',
          icon: 'contacts-line',
        },
      },
      {
        path: 'orderCodeList',
        name: 'OrderCodeList',
        component: () =>
          import('@/views/enterpriseWeChat/orderCodeList/index.vue'),
        meta: {
          hidden: true,
          title: '激活码',
          icon: 'file-list-3-line',
        },
      },
      {
        path: 'customer',
        name: 'Customer',
        component: () => import('@/views/enterpriseWeChat/customer/index.vue'),
        meta: {
          hidden: true,
          title: '客服',
          dynamicNewTab: true,
        },
      },
      {
        path: 'department',
        name: 'Department',
        component: () => import('@/views/enterpriseWeChat/department/index.vue'),
        meta: {
          hidden: true,
          title: '部门',
          dynamicNewTab: true,
        },
      },
    ],
  },
  {
    path: '/setting',
    name: 'Setting',
    component: Layout,
    meta: {
      title: '配置',
      icon: 'user-settings-line',
    },
    children: [
      {
        path: 'personalCenter',
        name: 'PersonalCenter',
        component: () => import('@/views/setting/personalCenter/index.vue'),
        meta: {
          title: '个人中心',
          icon: 'map-pin-user-line',
        },
      },
    ],
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404',
    name: 'NotFound',
    meta: {
      hidden: true,
    },
  },
]

const router = createRouter({
  history: isHashRouterMode
    ? createWebHashHistory(publicPath)
    : createWebHistory(publicPath),
  routes: constantRoutes as RouteRecordRaw[],
})

function fatteningRoutes(routes: VabRouteRecord[]): VabRouteRecord[] {
  return routes.flatMap((route: VabRouteRecord) => {
    return route.children ? fatteningRoutes(route.children) : route
  })
}

function addRouter(routes: VabRouteRecord[]) {
  routes.forEach((route: VabRouteRecord) => {
    if (!router.hasRoute(route.name)) router.addRoute(route as RouteRecordRaw)
    if (route.children) addRouter(route.children)
  })
}

export function resetRouter(routes: VabRouteRecord[] = constantRoutes) {
  routes.map((route: VabRouteRecord) => {
    if (route.children) route.children = fatteningRoutes(route.children)
  })
  router.getRoutes().forEach(({ name }) => {
    router.hasRoute(<RouteRecordName>name) &&
      router.removeRoute(<RouteRecordName>name)
  })
  addRouter(routes)
}

export function setupRouter(app: any) {
  if (authentication === 'intelligence') addRouter(asyncRoutes)
  setupPermissions(router)
  app.use(router)
  return router
}

export default router
